using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.kartoffsetdata
{
    public partial class kartoffsetdata : Form
    {
        string file;
        public kartoffsetdata(string file)
        {
            InitializeComponent();
            this.file = file;
        }
        kartoffsetdata_file kk;
        private string[] Karts =
        {
            "Standard MR",
            "Shooting Star",
            "B Dasher",
            "Standard Dk",
            "Whild Life",
            "Rambi Rider",
            "Standard TD",
            "Mushmellow",
            "4 Wheel Cradle",
            "Standard BW",
            "Hurricane",
            "Tyrant",
            "Standard PC",
            "Light Tripper",
            "Royale",
            "Standard WR",
            "Brute",
            "Dragonfly",
            "Standard YS",
            "Egg 1",
            "Cucumber",
            "Standard LG",
            "Poltergust 4000",
            "Streamliner",
            "Standard DB",
            "Dry Bomber",
            "Banisher",
            "Standard DS",
            "Light Dancer",
            "Power Flower",
            "Standard WL",
            "Gold Mantis",
            "Zipper",
            "Standard RB",
            "ROB-BLS",
            "ROB-LGS",
            "Standard HH"
        };
        private void kartoffsetdata_Load(object sender, EventArgs e)
        {
            EndianBinaryReader er = new EndianBinaryReader(File.OpenRead(file), Endianness.LittleEndian);
            kk = new kartoffsetdata_file();
            kk.Data = new kartoffsetdata_entry[37];
            for (int i = 0; i < 37; i++)
            {
                kk.Data[i] = new kartoffsetdata_entry();
                kk.Data[i].Type = er.ReadString(Encoding.ASCII, 16);
                kk.Data[i].Front_Size = (float)er.ReadInt32()/4096f;
                kk.Data[i].Front_Left.X = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Front_Left.Y = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Front_Left.Z = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Front_Right.X = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Front_Right.Y = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Front_Right.Z = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Left.X = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Left.Y = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Left.Z = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Right.X = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Right.Y = (float)er.ReadInt32() / 4096f;
                kk.Data[i].Back_Right.Z = (float)er.ReadInt32() / 4096f;
                for (int j = 0; j < 13; j++)
                {
                    kk.Data[i].Char[j] = new Character();
                    kk.Data[i].Char[j].X = (float)er.ReadInt32() / 4096f;
                    kk.Data[i].Char[j].Y = (float)er.ReadInt32() / 4096f;
                    kk.Data[i].Char[j].Z = (float)er.ReadInt32() / 4096f;
                }
            }
            er.Close();
            listBox1.Items.AddRange(Karts);
            listBox1.SelectedIndex = 0;
        }
        public struct kartoffsetdata_file
        {
            public kartoffsetdata_entry[] Data;
        }
        public class kartoffsetdata_entry
        {
            public string Type;
            public float Front_Size;
            public Wheel Front_Left = new Wheel();
            public Wheel Front_Right = new Wheel();
            public Wheel Back_Left = new Wheel();
            public Wheel Back_Right = new Wheel();
            public Character[] Char = new Character[13];
            public byte[] Unknown4;
        }
        public class Wheel
        {
            public float X;
            public float Y;
            public float Z;
        }
        public class Character
        {
            public float X;
            public float Y;
            public float Z;
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Text = kk.Data[listBox1.SelectedIndex].Type;
            numericUpDown1.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Size;
            numericUpDown2.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Left.X;
            numericUpDown3.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Left.Y;
            numericUpDown4.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Left.Z;

            numericUpDown7.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Right.X;
            numericUpDown6.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Right.Y;
            numericUpDown5.Value = (decimal)kk.Data[listBox1.SelectedIndex].Front_Right.Z;

            numericUpDown10.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Left.X;
            numericUpDown9.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Left.Y;
            numericUpDown8.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Left.Z;

            numericUpDown13.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Right.X;
            numericUpDown12.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Right.Y;
            numericUpDown11.Value = (decimal)kk.Data[listBox1.SelectedIndex].Back_Right.Z;

            comboBox2.SelectedIndex = 0;
            numericUpDown14.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[0].X;
            numericUpDown15.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[0].Y;
            numericUpDown16.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[0].Z;
        }

        private void comboBox1_TextUpdate(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Type = comboBox1.Text;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Size = (float)numericUpDown1.Value;
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Left.X = (float)numericUpDown2.Value;
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Left.Y = (float)numericUpDown3.Value;
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Left.Z = (float)numericUpDown4.Value;
        }

        private void numericUpDown7_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Right.X = (float)numericUpDown7.Value;
        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Right.Y = (float)numericUpDown6.Value;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Front_Right.Z = (float)numericUpDown5.Value;
        }

        private void numericUpDown10_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Left.X = (float)numericUpDown10.Value;
        }

        private void numericUpDown9_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Left.Y = (float)numericUpDown9.Value;
        }

        private void numericUpDown8_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Left.Z = (float)numericUpDown8.Value;
        }

        private void numericUpDown13_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Right.X = (float)numericUpDown13.Value;
        }

        private void numericUpDown12_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Right.Y = (float)numericUpDown12.Value;
        }

        private void numericUpDown11_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Back_Right.Z = (float)numericUpDown11.Value;
        }

        private void numericUpDown14_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].X = (float)numericUpDown14.Value;
        }

        private void numericUpDown15_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].Y = (float)numericUpDown15.Value;
        }

        private void numericUpDown16_ValueChanged(object sender, EventArgs e)
        {
            kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].Z = (float)numericUpDown16.Value;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            EndianBinaryWriter er = new EndianBinaryWriter(File.Create(file), Endianness.LittleEndian);
            for (int i = 0; i < 37; i++)
            {
                er.Write(kk.Data[i].Type, Encoding.ASCII, false);
                if (kk.Data[i].Type.Length != 16)
                {
                    er.Write(new byte[16 - kk.Data[i].Type.Length], 0, 16 - kk.Data[i].Type.Length);
                }
                er.Write((int)(kk.Data[i].Front_Size * 4096f));
                er.Write((int)(kk.Data[i].Front_Left.X * 4096f));
                er.Write((int)(kk.Data[i].Front_Left.Y * 4096f));
                er.Write((int)(kk.Data[i].Front_Left.Z * 4096f));
                er.Write((int)(kk.Data[i].Front_Right.X * 4096f));
                er.Write((int)(kk.Data[i].Front_Right.Y * 4096f));
                er.Write((int)(kk.Data[i].Front_Right.Z * 4096f));
                er.Write((int)(kk.Data[i].Back_Left.X * 4096f));
                er.Write((int)(kk.Data[i].Back_Left.Y * 4096f));
                er.Write((int)(kk.Data[i].Back_Left.Z * 4096f));
                er.Write((int)(kk.Data[i].Back_Right.X * 4096f));
                er.Write((int)(kk.Data[i].Back_Right.Y * 4096f));
                er.Write((int)(kk.Data[i].Back_Right.Z * 4096f));
                for (int j = 0; j < 13; j++)
                {
                    er.Write((int)(kk.Data[i].Char[j].X * 4096f));
                    er.Write((int)(kk.Data[i].Char[j].Y * 4096f));
                    er.Write((int)(kk.Data[i].Char[j].Z * 4096f));
                }
            }
            er.Close();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            numericUpDown14.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].X;
            numericUpDown15.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].Y;
            numericUpDown16.Value = (decimal)kk.Data[listBox1.SelectedIndex].Char[comboBox2.SelectedIndex].Z;
        }
    }
}
